﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Resources;
using System.Globalization;
using ArmoryViewer;
using System.Windows.Documents;
using Microsoft.WindowsAPICodePack.Shell;

namespace ArmoryViewer.Classes
{
    public static class LocalizationProvider
    {
        private static List<DependencyObject> _contentObjects;

        static LocalizationProvider()
        {
            _contentObjects = new List<DependencyObject>();
        }

        public static void UpdateAllObjects()
        {
            foreach (DependencyObject obj in _contentObjects)
            {
                ResourceManager res = App.ResourceManager;
                string key = obj.GetValue(ContentIDProperty) as string;
                string resourceValue = null;

                try { resourceValue = res.GetString(key, CultureInfo.CurrentCulture); }
                catch { resourceValue = key; }
                setValue(obj, resourceValue);  
            }
        }

        #region ContentID property
        public static string GetContentID(DependencyObject obj)
        {
            return (string)obj.GetValue(ContentIDProperty);
        }

        public static void SetContentID(DependencyObject obj, string value)
        {
            obj.SetValue(ContentIDProperty, value);
        }

        private static void OnContentIDChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            if (obj == null)
                throw new ArgumentNullException("obj");

            ResourceManager res = App.ResourceManager;
           
            string resourceValue = null;
            try {
                resourceValue = res.GetString(e.NewValue as string, CultureInfo.CurrentCulture);
            } catch { 
                resourceValue = (e.NewValue as string); 
            }

            if (resourceValue != null)
            {
                setValue(obj, resourceValue);   
                if (!_contentObjects.Contains(obj))
                    _contentObjects.Add(obj);
            }

        }

        private static void setValue(DependencyObject obj, string value)
        {
            if (obj is Window)
                (obj as Window).Title = value;
            else if (obj is HeaderedContentControl)
                (obj as HeaderedContentControl).SetValue(HeaderedContentControl.HeaderProperty, value);
            else if (obj is ContentControl)
                (obj as ContentControl).SetValue(ContentControl.ContentProperty, value);
            else if (obj is TextBox)
                (obj as TextBox).SetValue(TextBox.TextProperty, value);
            else if (obj is HeaderedItemsControl)
                (obj as HeaderedItemsControl).SetValue(HeaderedItemsControl.HeaderProperty, value);
            else if (obj is GridViewColumn)
                (obj as GridViewColumn).SetValue(GridViewColumn.HeaderProperty, value);
            else if (obj is Run)
                (obj as Run).Text = value;
            else
                throw new InvalidCastException(string.Format("Type '{0}' is not supported.", obj.GetType().FullName));
        }

        public static DependencyProperty ContentIDProperty =
            DependencyProperty.RegisterAttached("ContentID", typeof(string), typeof(LocalizationProvider),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsArrange, new PropertyChangedCallback(OnContentIDChanged)));
        #endregion
    }
}
